package com.tg.message.msg;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.base.BaseActivity;
import com.tange.base.toolkit.StatusBarUtil;
import com.tange.base.toolkit.StatusNavUtils;
import com.tg.appcommon.android.FixMemLeak;
import com.tg.appcommon.android.TGLog;
import com.tange.core.backend.service.ep.EnvironmentProxy;
import com.tg.message.R;

public class MessagePlayActivity extends BaseActivity {

    public static final String TAG_PLAY = "MessagePlay#";
    private static final String TAG = TAG_PLAY + "Activity";

    private MessagePlayerView messagePlayerView;
    private MessageInfoView messageInfoView;
    private MessageAIView messageAIView;

    private MessageDataHolder messageDataHolder;

    public static long pageStart;

    private Activity activity;

    public void setActivity(Activity activity){
        this.activity = activity;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindow();

        pageStart = System.currentTimeMillis();
        TGLog.i(TAG, "onCreate: ");

        messageDataHolder = new MessageDataHolder(this);

        initInternalView(savedInstanceState);

        // 先请求完整的设备信息，再渲染UI
        messageDataHolder.init(getIntent(), () -> fillPage());
    }

    private void initWindow() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_message_play);
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        window.getDecorView().setBackgroundColor(Color.BLACK);
        hideActionBar();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected int statusBarColor() {
        return Color.BLACK;
    }

    @Override
    protected void initView() {
    }

    private void initInternalView(Bundle savedInstanceState) {
        messagePlayerView = findViewById(R.id.top_view);
        messageAIView = findViewById(R.id.ai_view);
        messageInfoView = findViewById(R.id.bottom_view);
        messageInfoView.setActivity(this);
        messageInfoView.setSavedInstanceState(savedInstanceState);
        messageInfoView.setMessagePlayerView(messagePlayerView);

        messagePlayerView.setOnVideoReadyListener(new MessagePlayerView.OnVideoReadyListener() {
            @Override
            public void onVideoReady() {
                messageInfoView.switchToVideoDownloadMode();
            }
        });
    }

    private void fillPage() {
        TGLog.i(TAG, "fillPage ...");

        if (isDestroyed() || isFinishing()) {
            TGLog.i(TAG, "fillPage skip.");
            return;
        }

        final int displayType = messageDataHolder.getDisplayType();

        if (displayType == MessageDataHolder.MSG_EVENT_TYPE_MAP) {

            messagePlayerView.setVisibility(View.GONE);
            messageAIView.setVisibility(View.GONE);
            messageInfoView.setVisibility(View.VISIBLE);

            messageInfoView.refresh(messageDataHolder);

            StatusBarUtil.setLightStatusBar(this, false);

        } else if (displayType == MessageDataHolder.MSG_EVENT_TYPE_TEXT) {

            messagePlayerView.setVisibility(View.GONE);
            messageAIView.setVisibility(View.GONE);
            messageInfoView.setVisibility(View.VISIBLE);

            messageInfoView.refresh(messageDataHolder);

            StatusBarUtil.setLightStatusBar(this, true);
            StatusNavUtils.setStatusBarColor(this, getResources().getColor(com.module.commonui.R.color.global_customize_fixed_color_light_gray));

        } else  if (displayType == MessageDataHolder.MSG_EVENT_TYPE_AI) {

            messagePlayerView.setVisibility(View.VISIBLE);
            messageAIView.setVisibility(View.VISIBLE);
            messageInfoView.setVisibility(View.GONE);

            messageAIView.setMessagePlayManager(messageDataHolder);
            messageAIView.refresh();
            messagePlayerView.refresh(messageDataHolder);

            StatusBarUtil.setLightStatusBar(this, false);

        } else if (displayType == MessageDataHolder.MSG_EVENT_TYPE_VIDEO) { // 视频类型

            StatusBarUtil.setLightStatusBar(this, false);

            messagePlayerView.setVisibility(View.VISIBLE);
            messageAIView.setVisibility(View.GONE);
            messageInfoView.setVisibility(View.VISIBLE);

            messageInfoView.refresh(messageDataHolder);
            messagePlayerView.refresh(messageDataHolder);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        TGLog.i(TAG, "onResume");

        if (messagePlayerView != null && messagePlayerView.getVisibility() == View.VISIBLE) {
            messagePlayerView.resume();
        }

        if (messageInfoView != null) {
            messageInfoView.onResume();
        }
        EnvironmentProxy.initStatic();
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);

        if (messageInfoView != null) {
            messageInfoView.onSaveInstanceState(outState);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            TGLog.i(TAG, "onNewIntent intent = " + intent.getExtras());
        }
        messageDataHolder.init(intent, () -> fillPage());
    }

    @Override
    public void onBackPressed() {

        if (messagePlayerView.getVisibility() == View.VISIBLE) {
            messagePlayerView.onBackPressed();
        } else {
            messageDataHolder.finishPage(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        TGLog.i(TAG, "onPause");

        if (messageInfoView != null) {
            messageInfoView.onPause();
        }

        if (messagePlayerView.getVisibility() == View.VISIBLE) {
            messagePlayerView.pause();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        TGLog.i(TAG, "onDestroy");

        if (messageInfoView != null) {
            messageInfoView.onDestroy();
        }

        if (messagePlayerView.getVisibility() == View.VISIBLE) {
            messagePlayerView.destroy();
        }

        FixMemLeak.fixLeak(this);
    }
}
